﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;

namespace Navigant.Web.UI
{
    public class CustodianEmailTemplateField : ITemplate
    {
        private DataControlRowType rowType;
        
        public CustodianEmailTemplateField(DataControlRowType rowType)
        {
            this.rowType = rowType;
        }

        #region ITemplate Members

        public void InstantiateIn(Control container)
        {
            if (this.rowType == DataControlRowType.DataRow)
            {
                Literal email = new Literal();
                email.DataBinding += new EventHandler(email_DataBinding);
                container.Controls.Add(email);
            }
        }

        private void email_DataBinding(object sender, EventArgs e)
        {
            Literal email  = (Literal)sender;
            GridViewRow row = (GridViewRow)email.NamingContainer;
            email.Text = string.Empty;
            PreservationNotice notice = row.DataItem as PreservationNotice;
            if (notice != null && notice.To != null)
            {
                email.Text = notice.To.Email;
            }
        }

        #endregion
    }
}
